home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’97 / Warrior’s Progress / source code / Source / Libraries / Arrays / Array.h < prev    next >
Encoding:
Text File  |  1997-06-28  |  2.2 KB  |  71 lines  |  [TEXT/CWIE]

  1. // Array.h
  2.  
  3. #ifndef Array_h
  4. #define Array_h
  5.  
  6. #ifndef ArrayOf_h
  7. #include "ArrayOf.h"
  8. #endif
  9.  
  10. template < class Element, uint32 size >
  11. class Array
  12.   {
  13.     typedef ArrayOf<Element> ArrayType;
  14.     typedef ConstArrayOf<Element> ConstArrayType;
  15.     
  16.     private:
  17.         const ArrayType head;
  18.         struct
  19.           {
  20.             Element elements[ size ];
  21.           } body;
  22.         
  23.     public:
  24.         Array()
  25.           : head( body.elements, size )
  26.           {}
  27.         
  28.         operator const ArrayOf<Element>&()                        { return head; }
  29.         operator const ConstArrayOf<Element>&() const        { return head; }
  30.         
  31.         void operator=( const Array<Element, size>& r )        { body = r.body; }
  32.         
  33.         Element& operator[]( uint32 i )
  34.           { Assert( i < size ); return body.elements[ i ]; }
  35.  
  36.         const Element& operator[]( uint32 i ) const
  37.           { Assert( i < size ); return body.elements[ i ]; }
  38.  
  39.         uint32 Length() const                                        { return size; }
  40.         URange32 Range() const                                        { return URange32( 0, Length() ); }
  41.                 
  42.         uint32 BoundedLength( uint32 bound ) const
  43.             { return ( size <= bound ) ? size : bound; }
  44.         
  45.         bool IsEmpty() const                                            { return size == 0; }
  46.  
  47.         // These operators express the containment ordering
  48.         bool operator==( ConstArrayType r ) const                { return head == r; }
  49.         bool operator!=( ConstArrayType r ) const                { return head != r; }
  50.         bool operator>=( ConstArrayType r ) const                { return head >= r; }
  51.         bool operator>( ConstArrayType r ) const                { return head > r; }
  52.  
  53.         Element *Start()                                                { return body.elements; }
  54.         Element *End()                                                    { return body.elements+size; }
  55.         const Element *Start() const                                { return body.elements; }
  56.         const Element *End() const                                    { return body.elements + size; }
  57.         
  58.         ArrayType Head( uint32 position )                        { return head.Head( position ); }
  59.         ArrayType Tail( uint32 position )                        { return head.Tail( position ); }
  60.         ArrayType Middle( URange32 range )                        { return head.Middle( range ); }
  61.         
  62.         ConstArrayType Head( uint32 position ) const            { return head.Head( position ); }
  63.         ConstArrayType Tail( uint32 position ) const            { return head.Tail( position ); }
  64.         ConstArrayType Middle( URange32 range ) const        { return head.Middle( range ); }
  65.         
  66.         uint32 operator<<( ConstArrayType r )                    { return head << r; }
  67.         uint32 BitwiseCopy( ConstArrayType r )                    { return head.BitwiseCopy( r ); }
  68.   };
  69.  
  70. #endif
  71.